package com.foo.designpattern.proxy.dynamicproxy.cglibdynamicproxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * com.foo.designpattern.proxy.dynamicproxy.jdkdynamicproxy
 * Created with IntelliJ IDEA.
 * User: wangyuxi
 * DateTime: 2016/4/1722:45
 *
 * @desc cglib动态代理
 */
public class WangPo implements MethodInterceptor {

    private Enhancer enhancer = new Enhancer();

    public Object getProxy(Class clazz) {
        //设置需要创建子类的类
        //cglib是针对类实现代理s
        enhancer.setSuperclass(clazz);
        enhancer.setCallback(this);
        //通过字节码技术动态创建子类实例
        return enhancer.create();
    }


    //实现MethodInterceptor接口方法
    @Override
    public Object intercept(Object obj, Method method, Object[] args,
                            MethodProxy proxy) throws Throwable {
        //System.out.println("前置代理");
        //通过代理类调用父类中的方法
        Object result = proxy.invokeSuper(obj, args);
        //System.out.println("后置代理");
        return result;
    }
}
